package com.levi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.levi.mapper.OrderMapper;
import com.levi.pojo.Orders;
import com.levi.pojo.TrancationLog;
import com.levi.service.OrderService;
import com.levi.service.TrancationLogService;
import com.levi.vo.OrderDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author shentong
 * @since 2022/6/6 9:28 PM
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Resource
    private TrancationLogService trancationLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createOrder(OrderDto orderDto,String tracationId) {
        // TODO 创建订单过程中可能会出现异常
        Orders orders = new Orders();
        orders.setProductCount(orderDto.getProductCount());
        orders.setProductId(orderDto.getProductId());
        this.save(orders);
//        log.info("下单成功！！");
        System.out.println("下单成功！！");
        TrancationLog trancationLog = new TrancationLog();
        trancationLog.setBusiness("order");
        trancationLog.setForeignKey(orders.getId());
        trancationLog.setId(tracationId);
        boolean result = trancationLogService.save(trancationLog);
//        log.info("保存消息事务表成功");
        System.out.println("保存消息事务表成功！！");
        return true;
    }
}
